package com.beichuan_travel.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.beichuan_travel.enums.ResultCode;
import com.beichuan_travel.mapper.UserMapper;
import com.beichuan_travel.model.dto.LoginDTO;
import com.beichuan_travel.model.po.UserPO;
import com.beichuan_travel.model.vo.ResultVO;
import com.beichuan_travel.model.vo.UserVO;
import com.beichuan_travel.service.UserService;
import com.beichuan_travel.utils.JwtManager;
import com.beichuan_travel.utils.WeChatUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    WeChatUtils weChatUtils;

    @Resource
    JwtManager jwtManager;

    @Override
    public JSONObject queryUserById(String userId) {
        return null;
    }

    @Override
    public JSONObject queryUserByName(String userName) {
        return null;
    }

    @Override
    public JSONObject queryUserByWeixin(String weixin) {
        return null;
    }

    @Override
    public JSONObject addUser(UserPO userPO) {
        return null;
    }

    @Override
    public JSONObject deleteUser(UserPO userPO) {
        return null;
    }

    @Override
    public JSONObject updateUser(UserPO userPO) {
        return null;
    }

    @Override
    public UserVO login(LoginDTO loginDTO) {
        JSONObject jsonObject = weChatUtils.getSessionKeyOrOpenId(loginDTO.getCode());
        String session_key = jsonObject.getString("session_key");
        String open_id = jsonObject.getString("openid");

        UserPO userPO = userMapper.queryByOpenId(open_id);
        UserVO userVO = new UserVO();
        if (userPO != null) {
            userMapper.updateLoginTime(new Date(),session_key, open_id);
            userVO.setId(userPO.getId());
            userVO.setNickName(userPO.getNickName());
            userVO.setCity(userPO.getCity());
            userVO.setProvince(userPO.getProvince());
            userVO.setAvatarUrl(userPO.getAvatarUrl());
            userVO.setGender(userPO.getGender());
            userVO.setToken(jwtManager.generate(open_id));
        } else {
            UserPO insert = new UserPO();
            insert.setNickName(loginDTO.getNickName());
            insert.setOpenId(open_id);
            insert.setSessionKey(session_key);
            insert.setCreateTime(new Date());
            insert.setLastVisitTime(new Date());
            insert.setCity(loginDTO.getCity());
            insert.setProvince(loginDTO.getProvince());
            insert.setAvatarUrl(loginDTO.getAvatarUrl());
            insert.setGender(loginDTO.getGender());


            int i = userMapper.addUser(insert);
            if (i <= 0) {
                return null;
            }
            userVO.setId(insert.getId());
            userVO.setNickName(insert.getNickName());
            userVO.setCity(insert.getCity());
            userVO.setProvince(insert.getProvince());
            userVO.setAvatarUrl(insert.getAvatarUrl());
            userVO.setGender(insert.getGender());
            userVO.setToken(jwtManager.generate(open_id));
        }

        return userVO;

    }
}
